/* testmemory.c - testmemory */

#include <xinu.h>
#include <mips.h>
#include <string.h>
#include <stdio.h>

void pr1(void);
pid32 pid1;

/*--------------------------------------------------------------------------
 *  testmemory  -  benchmark the reclaim implementation
 *--------------------------------------------------------------------------
 */
void	testmemory(void)
{
	struct memblk *oldmnext;
	uint32 oldlen;
	oldmnext = memlist.mnext;
	oldlen = memlist.mlength;
	sleepms(100);
	pid1 = create(pr1, 1024, 20, "pr1", 0);
	resume(pid1);
	sleepms(100);
	sleepms(100);
	kill(pid1);
	if (memlist.mnext == oldmnext)
		printf("mnext is good.\r\n");
	if (memlist.mlength == oldlen)
		printf("length is good.\r\n");
}

void pr1(void)
{
	char *a;
	struct 	heapblk	*hprev, *hcurr, *htemp, *haddr;
	a = getmem(50);
	getmem(20);
	
	/*------added code starts here-----*/
	haddr = (struct heapblk *)((int)a - 16);
	hprev = &heaplist;
	hcurr = heaplist.hnext;
	while (hcurr != haddr) {
		hprev = hcurr;
		hcurr = hcurr->hnext;
	}
	htemp = (struct heapblk *)((int)hcurr + 16);
	htemp->hnext = hcurr->hnext;
	htemp->hlength = hcurr->hlength - 16;
	htemp->hpid = hcurr->hpid;
	htemp->sharec = hcurr->sharec;
	hcurr = htemp;
	hprev->hnext = hcurr;
	/*------added code ends here----*/

	freemem((char *)((int)a - 16), 10);
}
